class Solution(object):
    def numberOfSubstrings(self, s):
        left = 0
        n = len(s)
        right = 0
        d = {'a':0, 'b':0, 'c': 0}
        ans = 0
        while right < n:
            d[s[right]] += 1
            while d['a'] > 0 and d['b'] > 0 and d['c'] > 0:
                ans += n - right
                d[s[left]] -= 1
                left += 1
            right += 1
        return ans